package com.example.myapplication.activity;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.myapplication.R;
import com.example.myapplication.adapter.TeamsAdapter;
import com.example.myapplication.bean.TeamGsonData;
import com.example.myapplication.utils.OkHttpUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

public class TeamsActivity extends AppCompatActivity {
    private static final String TAG = "TeamsActivity";

    private Toolbar toolbar;
    private ListView listView;
    private Gson gson;
    private Context context = TeamsActivity.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teams);
        init();
        new Thread(){
            @Override
            public void run() {
                sendListReq();
            }
        }.start();
    }

    private void init() {
        toolbar = findViewById(R.id.tb_teams);
        listView = findViewById(R.id.list_team);
        gson = new Gson();
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    private void sendListReq() {
        JSONObject object = OkHttpUtil.post("/teams/list");
        final TeamGsonData teamGsonData = gson.fromJson(object.toString(), TeamGsonData.class);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context,teamGsonData.getMassage(), Toast.LENGTH_SHORT).show();
            }
        });
        if (teamGsonData.isSuccess()) {
            flashList(teamGsonData.getData());
        }
    }

    public void flashList(final List<TeamGsonData.Team> teams) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listView.setAdapter(new TeamsAdapter(TeamsActivity.this, teams));
                Log.d(TAG, "run: 刷新完成");
            }
        });
    }
}